package com.everflourish.yeah100.http;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.everflourish.yeah100.entity.question.CancelQuestion;
import com.everflourish.yeah100.entity.question.QuestionUploadImage;
import com.everflourish.yeah100.utils.constant.PaperType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考试题目接口请求封装
 *
 * @author pf
 * @date 2015-1-14
 */
public class QuestionRequest extends BaseRequest {
    private static QuestionRequest mRequest;

    private QuestionRequest() {
    }

    public static QuestionRequest getInstance() {
        if (mRequest == null) {
            mRequest = new QuestionRequest();
        }
        return mRequest;
    }

    /**
     * 题目列表获取
     *
     * @param startIndex
     * @param selectedCount
     * @param examinationId
     * @param paperType
     * @param searchText
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest questionListRequest(
            Integer startIndex, Integer selectedCount, String examinationId,
            PaperType paperType, String searchText,
            Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("examinationId", examinationId);
        if (startIndex != null) {
            map.put("startIndex", startIndex);
        }
        if (selectedCount != null) {
            map.put("selectCount", selectedCount);
        }
        if (paperType != null) {
            map.put("paperType", paperType.text);
        }
        if (searchText != null) {
            map.put("searchText", searchText);
        }
        JSONObject jsonObject = new JSONObject(map);
        String url = URL_NEW_QUESTION_TEACHER_LIST;
        request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 上传题目切割
     *
     * @param questionUploadImage
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest questionUpdateRequest(QuestionUploadImage questionUploadImage,
                                                   Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("examinationId", questionUploadImage.getExaminationId());
        map.put("questionId", questionUploadImage.getQuestionId());
//        map.put("seqNo", questionUploadImage.getSeqNo());
        map.put("paperType", questionUploadImage.getPaperType().text);
//        map.put("imageUrls", questionUploadImage.getImageUrls());
//        map.put("questionType", questionUploadImage.getQuestionType());
        map.put("comment", questionUploadImage.getComment());
        JSONObject jsonObject = new JSONObject(map);
        String url = URL_NEW_QUESTION_UPDATE;
        request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 上传题目切割
     *
     * @param questionUploadImage
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest questionUpLoadImageCutRequest(QuestionUploadImage questionUploadImage,
                                                           Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("examinationId", questionUploadImage.getExaminationId());
        map.put("seqNo", questionUploadImage.getSeqNo());
        map.put("paperType", questionUploadImage.getPaperType().text);
        map.put("compressWidth", questionUploadImage.getCompressWidth());
        map.put("width", questionUploadImage.getWidth());
        map.put("height", questionUploadImage.getHeight());
        map.put("leftMargin", questionUploadImage.getLeftMargin());
        map.put("topMargin", questionUploadImage.getTopMargin());
        map.put("imageUrl", questionUploadImage.getImageUrl());
        map.put("questionType", questionUploadImage.getQuestionType());
        map.put("role", questionUploadImage.getRole());
        JSONObject jsonObject = new JSONObject(map);
        String url = URL_NEW_QUESTION_IMAGE_CUT;
        request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS60,
                0, DEFAULT_BACKOFF_MULT));
        return request;
    }


    /**
     * 题目新增图片
     *
     * @param questionUploadImage
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest questionAddImageRequest(QuestionUploadImage questionUploadImage,
                                                     Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("examinationId", questionUploadImage.getExaminationId());
        map.put("questionId", questionUploadImage.getQuestionId());
        map.put("compressWidth", questionUploadImage.getCompressWidth());
        map.put("width", questionUploadImage.getWidth());
        map.put("height", questionUploadImage.getHeight());
        map.put("leftMargin", questionUploadImage.getLeftMargin());
        map.put("topMargin", questionUploadImage.getTopMargin());
        map.put("imageUrl", questionUploadImage.getImageUrl());
        JSONObject jsonObject = new JSONObject(map);
        String url = URL_NEW_QUESTION_ADD_IMAGE;
        request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS60,
                0, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 上传题目获取题号
     *
     * @param examinationId
     * @param paperType
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest getQuestionNosRequest(String examinationId, PaperType paperType,
                                                   Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("examinationId", examinationId);
        map.put("paperType", paperType.text);
        JSONObject jsonObject = new JSONObject(map);
        String url = URL_NEW_QUESTION_NOS;
        request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 删除题目
     *
     * @param examinationId
     * @param questionIds
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest deleteQuestionRequest(String examinationId, List<String> questionIds,
                                                   Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject();
            jsonObject.put("examinationId", examinationId);
            JSONArray jsonArray = new JSONArray(questionIds);
            jsonObject.put("questionIds", jsonArray);
            String url = URL_NEW_QUESTION_DELETE;
            request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                    errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 老师标签列表获取
     *
     * @param startIndex
     * @param selectCount
     * @param tagName
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest getQuestionTagListRequest(Integer startIndex, Integer selectCount, String tagName,
                                                       Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject();
            jsonObject.put("startIndex", startIndex);
            jsonObject.put("selectCount", selectCount);
            if (tagName != null) {
                jsonObject.put("tagName", tagName);
            }
            String url = URL_NEW_QUESTION_TAG_LIST;
            request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                    errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 老师收藏题目
     *
     * @param questionIds
     * @param tagNames
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest favoriteQuestionRequest(List<String> questionIds, List<String> tagNames,
                                                     Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject();
            JSONArray jsonArray1 = new JSONArray(questionIds);
            jsonObject.put("questionIds", jsonArray1);
            JSONArray jsonArray2 = new JSONArray(tagNames);
            jsonObject.put("tagNames", jsonArray2);
            String url = URL_NEW_QUESTION_TEACHER_FARORITE;
            request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                    errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }


    /**
     * 老师取消收藏题目
     *
     * @param cancelQuestions
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest unFavoriteQuestionRequest(List<CancelQuestion> cancelQuestions, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            for (CancelQuestion cancelQuestion : cancelQuestions) {
                JSONObject cancelQuestionObject = new JSONObject();
                cancelQuestionObject.put("questionId", cancelQuestion.getQuestionId());
                if (cancelQuestion.getTagIds() != null) {
                    JSONArray tagIdArray = new JSONArray(cancelQuestion.getTagIds());
                    cancelQuestionObject.put("tagIds", tagIdArray);
                }
                jsonArray.put(cancelQuestionObject);
            }
            jsonObject.put("cancelQuestions", jsonArray);
            String url = URL_NEW_QUESTION_TEACHER_UNFARORITE;
            request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                    errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 老师删除标签
     *
     * @param tagIds
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest deleteQuestionTagRequest(List<String> tagIds, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject();
            JSONArray jsonArray = new JSONArray(tagIds);
            jsonObject.put("tagIds", jsonArray);
            String url = URL_NEW_QUESTION_TAG_DELETE;
            request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                    errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 获取老师题库下的题目
     *
     * @param startIndex
     * @param selectedCount
     * @param tagId
     * @param searchText
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest getQuestionBankDetailRequest(
            Integer startIndex, Integer selectedCount, String tagId, String searchText,
            Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("tagId", tagId);
        if (startIndex != null) {
            map.put("startIndex", startIndex);
        }
        if (selectedCount != null) {
            map.put("selectCount", selectedCount);
        }
        if (searchText != null) {
            map.put("searchText", searchText);
        }
        JSONObject jsonObject = new JSONObject(map);
        String url = URL_NEW_QUESTION_TEACHER_TAG_QUESTION;
        request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }


    /**
     * 老师修改标签名
     *
     * @param tagId
     * @param tagName
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest updateQuestionLibTagRequest(String tagId, String tagName,
                                                         Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        JsonObjectRequest request = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tagId", tagId);
        map.put("tagName", tagName);
        JSONObject jsonObject = new JSONObject(map);
        String url = URL_NEW_QUESTION_TAG_UPDATE;
        request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }


}